<template>
  <div class="handle">
    <div class="title">
      <van-icon name="arrow-left" color="#ffffff" @click="skip_newProduct" />
      <van-col>卧室</van-col>
      <van-icon name="bars" color="#ffffff" @click="skip_storageRroom" />
    </div>
    <van-image
      class="photo"
      src="http://127.0.0.1:3000/newProduction/woshi.png"
    />
    <van-col >
      <ul class="bed">
        <li>
          <span>双人床</span>
          <van-icon name="arrow" color="#ff5555" />
        </li>
        <li>
          <div class="information" v-for="(item, index) in list1" :key="index"  @click="toGoodsDetails(item)">
            <van-image :src="item.showImg" />
            <span>{{ item.name }}</span>
            <p>${{ item.price }}</p>
          </div>
        </li>
      </ul>
      <ul class="bed">
        <li>
          <span>单人床</span>
          <van-icon name="arrow" color="#ff5555" />
        </li>
        <li>
          <div class="information" v-for="(item, index) in list2" :key="index" @click="toGoodsDetails(item)">
            <van-image :src="item.showImg" />
            <span>{{ item.name }}</span>
            <p>${{ item.price }}</p>
          </div>
        </li>
      </ul>
      <ul class="bed">
        <li>
          <span>抽屉柜</span>
          <van-icon name="arrow" color="#ff5555" />
        </li>
        <li>
          <div class="information" v-for="(item, index) in list3" :key="index" @click="toGoodsDetails(item)">
            <van-image :src="item.showImg" />
            <span>{{ item.name }}</span>
            <p>${{ item.price }}</p>
          </div>
        </li>
      </ul>
    </van-col>
  </div>
</template>

<script>
import {getBedroom} from "../../../api/index"
export default {
  data() {
    return {
      list1:[],
      list2:[],
      list3:[]
    };
  },
  mounted() {
    this.init()
  },
  methods: {
    async init() {
      let { data, meta: { status, msg },} = await getBedroom({id:1});
      // console.log(data);
      // 遍历商品分类
      data.map( (item, index) => {
        if(item.tags == '双人床') {
          this.list1.push(item)
        }else if( item.tags == '单人床' ) {
          this.list2.push(item)
        }else if( item.tags == '抽屉柜' ) {
          this.list3.push(item)
        }
      })
      // 如果得到的同一类商品数大于3则截取前三个展示
      if(this.list1.length > 3) {
        this.list1.splice(3)
        
      }
      if(this.list2.length > 3) {
        this.list2.splice(3)
      }
      if(this.list3.length > 3) {
        this.list3.splice(3)
      }
      

      
    },
    skip_newProduct() {
      this.$router.push("/new_product");
    },
    skip_storageRroom() {
      this.$router.push("/storage_room");
    },
    toGoodsDetails(data) {
      console.log(data);
      // 利用vuex存储当前点击项商品信息
      this.$store.commit('setCurrentGoods', data)
      // 跳转到商品详情页
      this.$router.push('/goodsDetails')
    }
  },
};
</script>

<style lang="less" scoped>
.handle {
  width: 100%;

  .title {
    height: 52px;
    background-color: #ff5555;
    padding: 0;
    padding-top: 16px;
    padding-bottom: 20px;
    display: flex;
    justify-content: space-between;
    .van-icon {
      padding: 10px 20px 0 16px;
      font-size: 42px;
    }
    .van-col {
      font-size: 18px;
      color: #ffffff;
      font-size: 34px;
      line-height: 54px;
    }
  }

  .photo {
    width: 100%;
    height: 470px;
  }
  .bed {
    margin-bottom: 60px;
    li:nth-child(1) {
      font-size: 26px;
      margin: 20px;
      display: flex;
      justify-content: space-between;
    }
    li:nth-child(2) {
      display: flex;
      margin: 0 15px;
      .information {
        width: 220px;
        min-height: 260px;
        font-size: 26px;
        margin: 0 10px;
        text-align: center;
        box-shadow: rgba(153, 153, 153, 0.5) 0px 0px 14px 0px;
        .van-image {
          width: 220px;
          height: 160px;
        }
        span {
          color: rgba(0, 0, 0, 0.8);
        }
        p {
          color: #ff5555;
          margin: 0;
          margin-top: 10px;margin-bottom: 26px;
        }
      }
    }
  }
}
</style>